package com.neusoft.chanceserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.neusoft.chanceserver.dto.PurchaseAdd;
import com.neusoft.chanceserver.dto.RespBean;
import com.neusoft.chanceserver.entity.ChancePurchase;
import com.neusoft.chanceserver.entity.Purchase;
import com.neusoft.chanceserver.mapper.ChancePurchaseMapper;
import com.neusoft.chanceserver.mapper.PurchaseMapper;
import com.neusoft.chanceserver.service.IPurchaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 购买决策人信息表 服务实现类
 * </p>
 *
 * @author liuxp
 * @since 2021-07-12
 */
@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> implements IPurchaseService {

    @Autowired
    PurchaseMapper purchaseMapper;

    @Autowired
    ChancePurchaseMapper chancePurchaseMapper;

    @Override
    public RespBean addPurchase(PurchaseAdd purchaseAdd) {
        RespBean respBean = RespBean.ok(200, "Purchase added successfully!");
        purchaseMapper.insert(purchaseAdd.getPurchase());
        int purchaseId = purchaseAdd.getPurchase().getPurchaseId();
        ChancePurchase chancePurchase = new ChancePurchase();
        chancePurchase.setChanceId(purchaseAdd.getChanceId());
        chancePurchase.setPurchaseId(purchaseId);
        chancePurchaseMapper.insert(chancePurchase);
        return respBean;
    }

    @Override
    public RespBean updatePurchase(Purchase purchase) {
        RespBean respBean = RespBean.ok(200, "Purchase updated successfully!");
        purchaseMapper.updateById(purchase);
        return respBean;
    }

    @Override
    public RespBean deletePurchase(Integer purchaseId) {
        RespBean respBean = RespBean.ok(200, "Purchase deleted successfully");
        purchaseMapper.deleteById(purchaseId);
        LambdaQueryWrapper<ChancePurchase> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ChancePurchase::getPurchaseId, purchaseId);
        chancePurchaseMapper.delete(wrapper);
        return respBean;
    }

    @Override
    public RespBean searchPurchaseByChanceId(Integer chanceId) {
        List<Integer> purchaseIds = new ArrayList<>();
        LambdaQueryWrapper<ChancePurchase> chancePurchaseLambdaQueryWrapper = Wrappers.lambdaQuery();
        chancePurchaseLambdaQueryWrapper.eq(ChancePurchase::getChanceId, chanceId);
        List<ChancePurchase> chancePurchases = chancePurchaseMapper.selectList(chancePurchaseLambdaQueryWrapper);
        for(ChancePurchase chancePurchase : chancePurchases){
            purchaseIds.add(chancePurchase.getPurchaseId());
        }
        List<Purchase> purchases = new ArrayList<>();
        if(purchaseIds.size() > 0)
            purchases = purchaseMapper.selectBatchIds(purchaseIds);
        RespBean respBean = RespBean.ok(200, "Find " + purchases.size() + " purchases!");
        respBean.setObj(purchases);
        return respBean;
    }


}
